<template>
  <div class="container">
    <div class="header">
      <el-select v-model="search.type" style="margin-bottom: 10px;" placeholder="请选择状态">
        <el-option
          v-for="item in typeList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
      <el-input
        style="width:200px"
        v-model="search.goods_code"
        placeholder="请输入商品名称"
        clearable
      ></el-input>
      <el-input
        style="width:200px"
        v-model="search.goods_name"
        placeholder="请输入商品Code"
        clearable
      ></el-input>

      <el-button type="primary" icon="el-icon-search" @click="handleSearch()"
        >查询</el-button
      >
      <el-button type="danger" icon="el-icon-delete" @click="empty()"
        >清空</el-button
      >

      <el-button type="info" icon="el-icon-upload" @click="derive()"
        >发票商品信息模板下载</el-button
      >
      <el-upload
        class="upload-demo"
        style="display: inline; margin-left: 10px"
        action="#"
        :show-file-list="false"
        :http-request="handleAvatarSuccess1"
      >
        <el-button
          type="primary"
          class="handleSearch"
          icon="el-icon-circle-plus"
          >导入生鲜发票商品信息</el-button
        >
      </el-upload>
      <el-upload
        class="upload-demo"
        style="display: inline; margin-left: 10px"
        action="#"
        :show-file-list="false"
        :http-request="handleAvatarSuccess2"
      >
        <el-button
          type="primary"
          class="handleSearch"
          icon="el-icon-circle-plus"
          >导入商超发票商品信息</el-button
        >
      </el-upload>
      <el-button type="primary" icon="el-icon-edit" @click="setUp()"
        >设置发票商品信息</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      @selection-change="handleSelectionChange"
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_code"
        label="商品编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="invoice_name"
        label="发票商品名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="invoice_spec"
        label="发票商品规格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="invoice_unit"
        label="发票商品单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type_msg"
        label="类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="添加时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="edit_time"
        label="编辑时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="edit_admin"
        label="编辑人"
        align="center"
      ></el-table-column>
      <!-- <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-warning-outline"
            @click="handleEdit(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <setBillBond :dialog="dialog" @upData="getData"></setBillBond>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getToken } from "@/utils/auth";
import SetBillBond from "@/components/bill/setBillBond";
import { getInvoiceGoods } from "@/api/bill";
import { Loading } from "element-ui";
export default {
  components: {
    pagination: Pagination,
    setBillBond: SetBillBond
  },
  data() {
    return {
      //页面数据
      tableData: [],
      typeList: [{ id: 1, name: "生鲜品" }, { id: 2, name: "商超品" }],
      search: {
        type: "",
        goods_code: "",
        goods_name: ""
      },
      multipleSelection: [],
      //dialog信息
      dialog: {
        dialogVisible: false
      },

      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      }
    };
  },
  created() {
    //页面数据
    this.getData();
  },
  methods: {
    setUp() {
      this.dialog.dialogVisible = true;
    },
    //导入发票商品
    handleAvatarSuccess1(param) {
      let loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)"
      });
      let data = new window.FormData();
      data.append("type", 1);
      data.append("goods_file", param.file);
      data.append("token", getToken());
      this.$axios.post("/api/Invoicexd/importInvoiceGoods", data).then(res => {
        loading.close();
        if (res.data.code == 0) {
          this.$message({
            message: res.data.message || res.data.msg,
            type: "success"
          });
          this.getData();
        } else {
          this.$message({
            type: "info",
            message: res.data.msg
          });
        }
      });
    },
    //导入商超发票商品
    handleAvatarSuccess2(param) {
      let loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)"
      });
      let data = new window.FormData();
      data.append("type", 2);
      data.append("goods_file", param.file);
      data.append("token", getToken());
      this.$axios.post("/api/Invoicexd/importInvoiceGoods", data).then(res => {
        loading.close();
        if (res.data.code == 0) {
          this.$message({
            message: res.data.message || res.data.msg,
            type: "success"
          });
          this.getData();
        } else {
          this.$message({
            type: "info",
            message: res.data.msg
          });
        }
      });
    },

    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        type: this.search.type,
        goods_code: this.search.goods_code,
        goods_name: this.search.goods_name
      };
      getInvoiceGoods(data).then(res => {
        res.data.paginate.data.forEach(item => {
          item.type_msg = item.type == 1 ? "生鲜" : "商超";
        });
        this.tableData = res.data.paginate.data;
        this.pageData.total = res.data.paginate.total;
      });
    },

    //编辑
    // handleEdit(scope) {
    //   this.dialog.dialogVisible = true;
    //   this.dialog.dialogType = "edit";
    //   this.dialog.id = scope.row.id;
    // },

    // 搜索
    handleSearch() {
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },
    //清空
    empty() {
      this.search = {
        type: "",
        goods_code: "",
        goods_name: ""
      };
    },
    // 是否发票商品信息模板下载
    derive() {
      this.$confirm("是否发票商品信息模板下载?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");

          link.href =
            this.$axios.defaults.baseURL +
            "/api/Invoicexd/downloadTemplte?token=" +
            getToken();

          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection, 8888);
    }
  }
};
</script>
<style scoped>
.header {
  margin-bottom: 10px;
}
.search {
  width: 200px;
  margin-right: 10px;
}
</style>
